package com.xianggu.designMode.iterationMethod.demo02;

import java.util.Iterator;
import java.util.NoSuchElementException;

// 具体聚合类
public class MyArrayList<E> implements MyList<E> {
    private Object[] data;
    private int size;

    @Override
    public Iterator<E> iterator() {
        return new MyIterator();
    }

    // 具体迭代器（内部类）
    private class MyIterator implements Iterator<E> {
        int cursor = 0;
        int lastRet = -1;

        @Override
        public boolean hasNext() {
            return cursor != size;
        }

        @Override
        public E next() {
            if (cursor >= size) throw new NoSuchElementException();
            lastRet = cursor;
            return (E) data[cursor++];
        }
    }
}